package edu.scu.declaresys.web.action;

import java.util.List;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;

import edu.scu.declaresys.config.IAttrName;
import edu.scu.declaresys.dao.util.Depth;
import edu.scu.declaresys.service.ServiceFactory;
import edu.scu.declaresys.service.intr.IAreaService;
import edu.scu.declaresys.vo.Area;
import edu.scu.declaresys.web.ABaseAction;

/**
 * 
 * @author: zt
 * @since : 2013-7-9
 */
public class AreaAction extends ABaseAction implements ModelDriven<Area>
{
	private static final long	serialVersionUID	= -4889548417205340423L;

	private IAreaService service = ServiceFactory.createAreaService();
	
	private Area vo = new Area();
	//
	// actions.
	//
	public String areaNextLevel() throws Exception
	{
		if(vo.getId() == null)
		{
			throw new Exception("未指定ID");
		}
		List<Area> areaLst = service.findAreaNextLevel(vo.getId());
		request.setAttribute(IAttrName.AREA_LIST, areaLst);
		return Action.SUCCESS;
	}
	
	public String areaDetail() throws Exception
	{
		if(vo.getId() == null)
		{
			throw new Exception("未指定ID");
		}
		vo = service.findAreaById(vo.getId(), Depth.ZERO);
		request.setAttribute(IAttrName.AREA_VO, vo);
		return Action.SUCCESS;
	}
	
	public String areaAllInOne() throws Exception
	{
		vo = service.findAreaById(Area.PROVINCE_ID, Depth.TWO);
		request.setAttribute(IAttrName.AREA_ALL_IN_ONE, vo);
		return Action.SUCCESS;
	}
	
	public String areaAdd() throws Exception
	{
		if(vo.getParentId() == null || vo.getName() == null)
		{
			throw new Exception("必须指定上级地区和地区名称");
		}
		//检查上级是否存在
		Area parent = service.findAreaById(vo.getParentId(), Depth.ZERO);
		if(parent == null)
		{
			throw new Exception("上级地区不存在");
		}
		//检查同级是否重复
		Area area = service.findByNameAndParent(vo.getParentId(), vo.getName());
		if(area != null)
		{
			throw new Exception("地区重复！");
		}
		// do add
		vo.setLevel(parent.getLevel() + 1);
		request.setAttribute(IAttrName.AREA_VO, 
				service.findAreaById(service.addArea(vo), Depth.ZERO));
		return Action.SUCCESS;
	}
	
	public String areaModify() throws Exception
	{
		if(vo.getName() != null)
		{
			if(vo.getId() == null)
			{
				throw new Exception("未指定地区");
			}
			else
			{
				//检查上级是否存在
				Area self = service.findAreaById(vo.getId(), Depth.ONE);
				if(self == null || self.getParent() == null)
				{
					throw new Exception("地区或上级地区不存在");
				}
				//检查同级是否重复
				Area area = service.findByNameAndParent(vo.getParentId(), vo.getName());
				if(area != null)
				{
					throw new Exception("地区重复！");
				}
				vo.setParentId(null);
				vo.setLevel(null);
				service.modifyArea(vo, vo.getId());
			}
		}		
		return Action.SUCCESS;
	}
	
	public String areaDelete() throws Exception
	{
		if(vo.getId() == null)
		{
			throw new Exception("未指定地区");
		}
		// Check child area.
		if(service.findAreaNextLevel(vo.getId()).isEmpty() == false)
		{
			throw new Exception("存在子地区，不能删除");
		}
		// Check user
		if(ServiceFactory.createUserService().hasUserInArea(vo.getId()))
		{
			throw new Exception("该地区下有用户，不能删除");
		}
		// Check project.
		if(ServiceFactory.createProjectService().hasProjectInArea(vo.getId()))
		{
			throw new Exception("该地区下有项目，不能删除");
		}
	
		// Do delete
		service.deleteArea(vo.getId());
		return Action.SUCCESS;
	}
	
	
	//
	// Getters and Setters
	// 
	@Override
	public Area getModel()
	{
		return vo;
	}

	public Area getArea()
	{
		return vo;
	}

	public void setArea(Area area)
	{
		this.vo = area;
	}
	
	
}
